﻿using Api.Common;
using Api.DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Api.DAL
{
    /// <summary>
	/// 数据访问类:wf_step
	/// </summary>
	public partial class wf_step
    {
        public wf_step()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from eazy_wf_step");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@ID", SqlDbType.Int,4)
            };
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id, int wf_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from eazy_wf_step");
            strSql.Append(" where ID=@ID and Instance_id=@Instance_id");
            SqlParameter[] parameters = {
                    new SqlParameter("@ID", SqlDbType.Int,4),
                    new SqlParameter("@Instance_id", SqlDbType.Int,4)
            };
            parameters[0].Value = id;
            parameters[0].Value = wf_id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Api.Models.wf_step model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into eazy_wf_step(");
            strSql.Append("WF_Instance,StepName,ProcessBy,Status,IsStartStep,IsEndStep,Result,Comment,SubTime,Checktime,DelFlag,Remark,Instance_id,submitBy,StepAnotherName,Isback,value)");
            strSql.Append(" values (");
            strSql.Append("@WF_Instance,@StepName,@ProcessBy,@Status,@IsStartStep,@IsEndStep,@Result,@Comment,@SubTime,@Checktime,@DelFlag,@Remark,@Instance_id,@submitBy,@StepAnotherName,@Isback,@value)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@WF_Instance", SqlDbType.Int,4),
                    new SqlParameter("@StepName", SqlDbType.NVarChar,150),
                    new SqlParameter("@ProcessBy", SqlDbType.Int,4),
                    new SqlParameter("@Status", SqlDbType.Int,4),
                    new SqlParameter("@IsStartStep", SqlDbType.Bit,1),
                    new SqlParameter("@IsEndStep", SqlDbType.Bit,1),
                    new SqlParameter("@Result", SqlDbType.NVarChar,250),
                    new SqlParameter("@Comment", SqlDbType.NVarChar,350),
                    new SqlParameter("@SubTime", SqlDbType.DateTime),
                    new SqlParameter("@Checktime", SqlDbType.DateTime),
                    new SqlParameter("@DelFlag", SqlDbType.Int,4),
                    new SqlParameter("@Remark", SqlDbType.NVarChar,510),
                    new SqlParameter("@Instance_id", SqlDbType.Int,4),
                    new SqlParameter("@submitBy", SqlDbType.Int,4),
                    new SqlParameter("@StepAnotherName", SqlDbType.NVarChar,150),
                    new SqlParameter("@Isback", SqlDbType.Bit,1),
                    new SqlParameter("@Value",SqlDbType.Int,4)};
            parameters[0].Value = model.WF_Instance;
            parameters[1].Value = model.StepName;
            parameters[2].Value = model.ProcessBy;
            parameters[3].Value = model.Status;
            parameters[4].Value = model.IsStartStep;
            parameters[5].Value = model.IsEndStep;
            parameters[6].Value = model.Result;
            parameters[7].Value = model.Comment;
            parameters[8].Value = model.SubTime;
            parameters[9].Value = model.Checktime;
            parameters[10].Value = model.DelFlag;
            parameters[11].Value = model.Remark;
            parameters[12].Value = model.instance_id;
            parameters[13].Value = model.submitBy;
            parameters[14].Value = model.stepAnotherName;
            parameters[15].Value = model.isback;
            parameters[16].Value = model.value;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Api.Models.wf_step model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update eazy_wf_step set ");
            strSql.Append("WF_Instance=@WF_Instance,");
            strSql.Append("StepName=@StepName,");
            strSql.Append("ProcessBy=@ProcessBy,");
            strSql.Append("Status=@Status,");
            strSql.Append("IsStartStep=@IsStartStep,");
            strSql.Append("IsEndStep=@IsEndStep,");
            strSql.Append("Result=@Result,");
            strSql.Append("Comment=@Comment,");
            strSql.Append("SubTime=@SubTime,");
            strSql.Append("Checktime=@Checktime,");
            strSql.Append("DelFlag=@DelFlag,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("StepAnotherName=@StepAnotherName,");
            strSql.Append("Value=@Value");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@WF_Instance", SqlDbType.Int,4),
                    new SqlParameter("@StepName", SqlDbType.NVarChar,150),
                    new SqlParameter("@ProcessBy", SqlDbType.Int,4),
                    new SqlParameter("@Status", SqlDbType.Int,4),
                    new SqlParameter("@IsStartStep", SqlDbType.Bit,1),
                    new SqlParameter("@IsEndStep", SqlDbType.Bit,1),
                    new SqlParameter("@Result", SqlDbType.NVarChar,250),
                    new SqlParameter("@Comment", SqlDbType.NVarChar,350),
                    new SqlParameter("@SubTime", SqlDbType.DateTime),
                    new SqlParameter("@Checktime", SqlDbType.DateTime),
                    new SqlParameter("@DelFlag", SqlDbType.Int,4),
                    new SqlParameter("@Remark", SqlDbType.NVarChar,510),
                    new SqlParameter("@StepAnotherName", SqlDbType.NVarChar,510),
                    new SqlParameter("@Value", SqlDbType.Int,4),
                    new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = model.WF_Instance;
            parameters[1].Value = model.StepName;
            parameters[2].Value = model.ProcessBy;
            parameters[3].Value = model.Status;
            parameters[4].Value = model.IsStartStep;
            parameters[5].Value = model.IsEndStep;
            parameters[6].Value = model.Result;
            parameters[7].Value = model.Comment;
            parameters[8].Value = model.SubTime;
            parameters[9].Value = model.Checktime;
            parameters[10].Value = model.DelFlag;
            parameters[11].Value = model.Remark;
            parameters[12].Value = model.stepAnotherName;
            parameters[13].Value = model.value;
            parameters[14].Value = model.ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 根据sql语句更新一个实例记录
        /// </summary>
        public int updateinstance(string str)
        {
            return DbHelperSQL.ExecuteSql(str);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from eazy_wf_step ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@ID", SqlDbType.Int,4)
            };
            parameters[0].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from eazy_wf_step ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Api.Models.wf_step GetModel(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,WF_Instance,StepName,ProcessBy,Status,IsStartStep,IsEndStep,Result,Comment,SubTime,Checktime,DelFlag,Remark,Instance_id,StepAnotherName,submitBy,Isback,value from eazy_wf_step ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@ID", SqlDbType.Int,4)
            };
            parameters[0].Value = ID;

            Api.Models.wf_step model = new Api.Models.wf_step();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Api.Models.wf_step GetCurrentModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,WF_Instance,StepName,ProcessBy,Status,IsStartStep,IsEndStep,Result,Comment,SubTime,Checktime,DelFlag,Remark,Instance_id,StepAnotherName,submitBy,value,Isback from eazy_wf_step ");
            strSql.Append(" where Instance_id=@Instance_id and Status=0");
            SqlParameter[] parameters = {
                    new SqlParameter("@Instance_id", SqlDbType.Int,4)
            };
            parameters[0].Value = ID;

            Api.Models.wf_step model = new Api.Models.wf_step();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Api.Models.wf_step DataRowToModel(DataRow row)
        {
            Api.Models.wf_step model = new Api.Models.wf_step();
            if (row != null)
            {
                if (row["ID"] != null && row["ID"].ToString() != "")
                {
                    model.ID = int.Parse(row["ID"].ToString());
                }
                if (row["WF_Instance"] != null && row["WF_Instance"].ToString() != "")
                {
                    model.WF_Instance = Int32.Parse(row["WF_Instance"].ToString());
                }
                if (row["StepName"] != null)
                {
                    model.StepName = row["StepName"].ToString();
                }
                if (row["ProcessBy"] != null && row["ProcessBy"].ToString() != "")
                {
                    model.ProcessBy = int.Parse(row["ProcessBy"].ToString());
                }
                if (row["Status"] != null && row["Status"].ToString() != "")
                {
                    model.Status = int.Parse(row["Status"].ToString());
                }
                if (row["IsStartStep"] != null && row["IsStartStep"].ToString() != "")
                {
                    if ((row["IsStartStep"].ToString() == "1") || (row["IsStartStep"].ToString().ToLower() == "true"))
                    {
                        model.IsStartStep = true;
                    }
                    else
                    {
                        model.IsStartStep = false;
                    }
                }
                if (row["IsEndStep"] != null && row["IsEndStep"].ToString() != "")
                {
                    if ((row["IsEndStep"].ToString() == "1") || (row["IsEndStep"].ToString().ToLower() == "true"))
                    {
                        model.IsEndStep = true;
                    }
                    else
                    {
                        model.IsEndStep = false;
                    }
                }
                if (row["Result"] != null)
                {
                    model.Result = row["Result"].ToString();
                }
                if (row["Comment"] != null)
                {
                    model.Comment = row["Comment"].ToString();
                }
                if (row["SubTime"] != null && row["SubTime"].ToString() != "")
                {
                    model.SubTime = DateTime.Parse(row["SubTime"].ToString());
                }
                if (row["Checktime"] != null && row["Checktime"].ToString() != "")
                {
                    model.Checktime = DateTime.Parse(row["Checktime"].ToString());
                }
                if (row["DelFlag"] != null && row["DelFlag"].ToString() != "")
                {
                    model.DelFlag = int.Parse(row["DelFlag"].ToString());
                }
                if (row["Remark"] != null)
                {
                    model.Remark = row["Remark"].ToString();
                }
                if (row["instance_id"] != null && row["instance_id"].ToString() != "")
                {
                    model.instance_id = int.Parse(row["instance_id"].ToString());
                }
                if (row["StepAnotherName"] != null)
                {
                    model.stepAnotherName = row["StepAnotherName"].ToString();
                }
                if (row["submitBy"] != null && row["submitBy"].ToString() != "")
                {
                    model.submitBy = int.Parse(row["submitBy"].ToString());
                }
                if (row["Isback"] != null && row["Isback"].ToString() != "")
                {
                    model.isback = Convert.ToBoolean(row["Isback"].ToString());
                }
                if (row["value"] != null && row["value"].ToString() != "")
                {
                    model.value = int.Parse(row["value"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,WF_Instance,StepName,ProcessBy,Status,IsStartStep,IsEndStep,Result,Comment,SubTime,Checktime,DelFlag,Remark,Instance_id,StepAnotherName,submitBy,Isback,value ");
            strSql.Append(" FROM eazy_wf_step ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM eazy_wf_step where Instance_id=" + id);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,WF_Instance,StepName,ProcessBy,Status,IsStartStep,IsEndStep,Result,Comment,SubTime,Checktime,DelFlag,Remark,Instance_id,StepAnotherName,submitBy,value ");
            strSql.Append(" FROM eazy_wf_step ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM eazy_wf_step ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetCount(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM eazy_wf_step ");
            strSql.Append(" where instance_id=" + id);
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from eazy_wf_step T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM eazy_wf_step");
            strSql.Append(" where " + strWhere);
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
        #endregion  BasicMethod
    }
}
